/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.lium.spkDiarization.lib;

import fr.lium.spkDiarization.libClusteringData.Cluster;
import fr.lium.spkDiarization.libClusteringData.ClusterSet;
import fr.lium.spkDiarization.libClusteringData.Segment;
import fr.lium.spkDiarization.parameter.Parameter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.UUID;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;

/**
 *
 * @author ryan
 */
public class EckoitTools {

        private static String SEPERATOR = "\t";

        public static void writeClusterSet( ClusterSet clusters, File file ) throws IOException {
            OutputStreamWriter dos = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("ISO-8859-1"));
            writeClusterSet(clusters, dos);
        }

	public static void writeClusterSet( ClusterSet clusters, OutputStreamWriter writer ) throws IOException {

                Date date = new Date();
                //writer.write("; " + date);

                for(Cluster cluster : clusters.clusterSetValue()) {
                    String gender = cluster.getGender();
                    String name = cluster.getName();
                    for (Iterator<Segment> i = cluster.iterator();i.hasNext();) {
                        Segment segment = i.next();
                        if (segment.getUuid() == null) {
                            segment.setUuid(UUID.randomUUID().toString());
                        }
                        segment.getStartInSecond();
                        segment.getLengthInSecond();
                        String line = segment.getUuid() + SEPERATOR + gender + "-" + name + SEPERATOR + segment.getStartInSecond() + SEPERATOR + segment.getLengthInSecond() + "\n";
                        writer.write(line);
                    }

                }
                writer.flush();
                writer.close();
            
	}

        public static File generateFileName(Parameter param) {
            String segOutFilename = IOFile.getFilename( param.parameterSegmentationOutputFile.getMask(), param.show);
            return  new File(segOutFilename);
        }
}
